// 导入db模块
const db = require('../config/db');
// 文章类
class Article {
    // 发布文章
    // article_id, classify_text, article_title, article_content,article_create_time,article_author,article_address
    publish(article_id, classify_text, article_title, article_content, article_create_time, article_author, article_address, article_src,classify_title,user_id) {
        // console.log({article_id, classify_text, article_title, article_content, article_create_time, article_author, article_address, article_src,classify_title,user_id})
        return new Promise((resolve, reject) => {
            let sql = `select article_title from db_article where article_title = '${article_title}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            // 发布文章
                            let sql2 = `insert into db_article(article_id, classify_text, article_title, article_content,article_create_time,article_author,article_address,article_src,classify_title,user_id) 
                         values('${article_id}','${classify_text}','${article_title}','${article_content}','${article_create_time}','${article_author}','${article_address}','${article_src}','${classify_title}','${user_id}')`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        resolve({
                                            code: 200,
                                            msg: '发布文章成功。'
                                            // result
                                        })
                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '发布文章发生异常。'
                                        })
                                    }
                                )

                        }
                        else {
                            // 文章标题已经存在
                            resolve({
                                code: 1,
                                msg: '该标题已经存在。请换一个'
                            })
                        }
                    },
                    () => {
                        reject({
                            code: -1,
                            msg: '查询文章标题失败。'
                        })
                    }
                )
        })
    }

    // 删除文章
    delete(id, article_id) {
        return new Promise((resolve, reject) => {
            let sql = `select id from db_article where id = '${id}'`
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该文章不存在了。'
                            })
                        }
                        else {
                            // 删除文章
                            let sql2 = `delete from db_article where id = '${id}' and article_id = '${article_id}'`
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log("1.0",result);
                                        if (result.affectedRows === 1) {
                                            resolve({
                                                code: 200,
                                                msg: '删除文章成功。'
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '删除文章失败。1111'
                                            })
                                        }
                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '删除文章失败。2222'
                                        })
                                    }
                                )
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询文章失败。',
                            err
                        })
                    }
                )
        })
    }

    // 文章列表
    // 分类文章
    list(classify_text, page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_article where classify_text = '${classify_text}' limit ${page_num} , ${page_size}`;
            let sql2 = `select count(*) as total from db_article where classify_text = '${classify_text}'`

            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取文章列表成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取文章列表失败。',
                            err
                        })
                    }
                )
        })
    }

    // 全部文章
    items(page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_article limit ${page_num}, ${page_size}`;
            let sql2 = `select count(*) as total from db_article`
            // resolve({page_num, page_size})
            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取文章列表成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取文章列表失败。',
                            err
                        })
                    }
                )
        })
    }
    // 文章详情
    detail(article_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_article where article_id = '${article_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        resolve({
                            code: 200,
                            msg: '获取文章详情成功。',
                            result: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '获取文章详情失败。',
                            err
                        })
                    }
                )
        })
    }
    // 搜索文章
    search(keyword) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_article where article_title like '%${keyword}%' or 
            article_author like '%${keyword}%'`;
            db.query(sql)
                .then(
                    (rows) => {
                        resolve({
                            code: 200,
                            msg: `关键字“${keyword}”搜索的结果`,
                            result: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '关键字搜索失败。'
                        })
                    },
                )
        })
    }

    // 获取分类对应的文章数量
    total_article_classify(classify_text) {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_article where classify_text = '${classify_text}'`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计${classify_text}文章数量成功。`,
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 获取所有文章数量
    total() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_article`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计文章数量成功。`,
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }
}

// 暴露文章类在系统对象下
module.exports = new Article();